package address

import (
	"context"
	"encoding/json"

	"laundry-service/app/client/internal/svc"
	"laundry-service/app/client/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAddressListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAddressListLogic {
	return &GetAddressListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAddressListLogic) GetAddressList(req *types.Request) (resp []*types.CreateAddressResp, err error) {
	userId, _ := l.ctx.Value("userId").(json.Number).Int64()

	addresses, length, err := l.svcCtx.Db.AddrList(userId)
	if err != nil {
		return []*types.CreateAddressResp{}, err
	}

	responList := make([]*types.CreateAddressResp, length)

	for i, address := range addresses {
		addr := new(types.CreateAddressResp)
		addr.Region = address.Region
		addr.Detail = address.Detail
		addr.Phone = address.Phone
		addr.Name = address.Name
		addr.Id = address.ID
		addr.PostalCode = address.PostalCode
		addr.CreatedAt = address.CreatedAt.Format("2006-01-02 15:04:05")
		addr.UpdatedAt = address.UpdatedAt.Format("2006-01-02 15:04:05")
		addr.Default = address.Default
		responList[i] = addr
	}

	return responList, nil
}
